package com.enterprise.support.utility.generator.pub;

import com.enterprise.application.mvc.struts.view.freemarker.BeanWrapper;
import com.enterprise.support.utility.Enterprise;
import freemarker.template.Template;
import org.apache.log4j.Logger;

import java.io.*;
import java.util.HashMap;


/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2011-1-31
 * Time: 17:11:49
 * To change this template use File | Settings | File Templates.
 */
public class FreemarkerManager {
    private final static Logger logger = Logger.getLogger(FreemarkerManager.class);
    private final static freemarker.template.Configuration configuration = new freemarker.template.Configuration();

    static {
        configuration.setClassForTemplateLoading(FreemarkerManager.class, "/config/generator/");
        configuration.setObjectWrapper(new BeanWrapper());
        configuration.setTemplateUpdateDelay(0);
    }

    public static boolean builerTemplate(String templateFile, final Enterprise params, String savepath) {
        Writer out = null;
        try {
            Template template = configuration.getTemplate(templateFile);
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(savepath), "UTF-8"));
            template.process(new HashMap() {{
                put("input", params);
            }}, out);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                out.flush();
                out.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return true;
    }

    public static void main(String[] args) {
        FreemarkerManager.builerTemplate("Entity.ftl", new Enterprise(), "E:\\MY_PROJECT\\enterprise_tools\\java\\src\\config\\tools\\entity2.ftl");
    }
}
